﻿// ## 2023/05/03 # 铃 # 修改了难度梯度影响.优化算法. ##
// ## 2023/01/23 # 铃 # 根据城市的人口特征,再次优化算法 ##
// ## 2022/12/29 # 铃 # 再次优化人口系统,同时优化资金算法,使得收入更平滑 ##
// ## 2022/11/02 # 铃 # 修改了资金收入算法,以适配新的人口系统 ##
// ## 2022/08/08 # 铃 # 把城市收入赋予结构体 ##
// ## 2022/02/17 # 江东新风 # 探索涨基础金收入 ##
// ## 2022/02/14 # 江东新风 # 部分常量中文化 ##
// ## 2021/10/29 # 江东新风 # 结构体存储调用方式改进 ##
// ## 2021/10/10 # 江东新风 # 人口相关设定 ##
// ## 2021/10/01 # 江东新风 # namespace的韩文改成英文 ##
// ## 2021/02/17 # 江东新风 # 城市数惩罚设置上限，调整系数 ##
// ## 2020/12/12 # 江东新风 # 修复trace参数报错 ##
// ## 2020/10/07 # 氕氘氚 # 修复造币厂未建成就生效的bug ##
// ## 2020/09/28 # 氕氘氚 # 造币允许不相邻、造币对鱼市场、大市场生效，玩家电脑分别设置倍率 ##
// ## 2020/09/05 # 氕氘氚 #变法bug修复##
// ## 2020/08/19 # 江东新风 #新特技-变法##
// ## 2020/08/10 # 氕氘氚 ##


namespace CITY_REVENUE
{
	const bool 调试模式 = false;
	class Main
	{
		Main()
		{
			pk::set_func(150, pk::func150_t(callback));
		}

		int callback(pk::city @city)
		{

			if (city is null or !pk::is_valid_force_id(city.get_force_id()))
				return 0;

			pk::force @force = pk::get_force(city.get_force_id());
			// 城市基础收入
			int n = city.revenue;
			int population_yield = 0;
			int market_yield = 0;

			int city_id = city.get_id();
			auto base_t = ch::get_base_p(city_id);
			float population = float(base_t.population + base_t.mil_pop_all);

			population_yield = inter::get_population_revenue(city_id);

			pk::int_int yield = inter::cal_gold_yield(city);
			market_yield = yield.second;
			int basic_yield = yield.first;

			// 每30的市场基础收入需要1万人维持，人数每低于要求1%，产量下降1%,最低倍率0.4，最高1.2



			float 市场效率 = inter::get_market_efficiency(population, basic_yield);

			market_yield = int(market_yield * 市场效率);

			n = population_yield + market_yield;

			if (调试模式)
				pk::trace(pk::format("{}=> revenue:{},population:{},population_yield:{},market_yield:{}",pk::decode(pk::get_name(city)), n, population, population_yield, market_yield));

			if (金粮收入_玩家_城市数_惩罚 and city.is_player() and !pk::is_campaign())
			{
				
				float force_city_count = float(pk::get_city_list(force).count);
				n = int(n * (1.f - pk::min(0.3f, (force_city_count - 3) * 0.015f)));
			}

			pk::building@ building = pk::city_to_building(city);

			int buff = 0;
			if (city_tech::is_force_has_city(force, 39))
				buff = pk::max(5, buff);

			if (city_tech::is_force_has_city(force, 6))
				buff = pk::max(5, buff);

			n = int(n * (100 + buff) / 100);

			// 全局修正
			n = inter::incomes_global_impact(n, building, 0);

			n += ch::get_base_p(city.get_id()).revenue_bonus;

			int base_revenue = n;
			
			// 下方函数用于获取城市收支，以便计算内政自动化各类花费---可以考虑是否有更好的写法
			pk::bool_bool skill_check = ch::has_skill(pk::city_to_building(city), 特技_富豪, 特技_征税);
			bool 富豪特技 = skill_check.first;
			bool 征税特技 = skill_check.second;

			if (富豪特技 and !征税特技) base_revenue = int(base_revenue * 1.5);

			if (!富豪特技 and 征税特技) base_revenue = int(base_revenue * 0.5 * 3);

			if (富豪特技 and 征税特技) base_revenue = int(base_revenue * 0.5) + int(base_revenue * 0.5) + int(base_revenue * 0.5 * 1.5);

			base_t.base_revenue = uint32(base_revenue);

			return n;
		}

	}

	Main main;

	int get_revenue(pk::city @city)
	{
		return main.callback(city);
	}

	int get_infact_revenue(pk::city @city)
	{
		main.callback(city);
		auto base_t = ch::get_base_p(city.get_id());
		return base_t.base_revenue;
	}
}